package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.SysAudit;
import com.settlement.system.model.entity.SysAuditRule;
import com.settlement.system.model.form.AuditRuleForm;
import com.settlement.system.model.form.UserForm;
import com.settlement.system.model.query.AuditPageQuery;
import com.settlement.system.model.query.AuditRulePageQuery;
import com.settlement.system.service.SysAuditRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 标准核销表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-08-14
 */
@Tag(name = "稽核规则")
@RestController
@RequestMapping("/api/v1/auditRule")
@RequiredArgsConstructor
public class SysAuditRuleController {

    private final SysAuditRuleService sysAuditRuleService;

    @Operation(summary = "稽核规则分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<SysAuditRule> getAuditPage(@ParameterObject AuditRulePageQuery queryParams) {
        Page<SysAuditRule> result = sysAuditRuleService.getAuditRulePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增规则", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:auditRule:add')")
    @PreventDuplicateSubmit
    public Result<?> saveRule(@RequestBody @Valid AuditRuleForm ruleForm) {
        boolean result = sysAuditRuleService.saveRule(ruleForm);
        return Result.judge(result);
    }

    @Operation(summary = "规则表单数据", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{ruleId}/form")
    public Result<AuditRuleForm> getRuleForm(@Parameter(description = "规则ID") @PathVariable Long ruleId) {
        AuditRuleForm formData = sysAuditRuleService.getRoleFormData(ruleId);
        return Result.success(formData);
    }

    @Operation(summary = "修改规则", security = {@SecurityRequirement(name = "Authorization")})
    @PutMapping(value = "/{ruleId}")
    @PreAuthorize("@ss.hasPerm('sys:auditRule:edit')")
    public Result<?> updateRule(@Parameter(description = "规则ID") @PathVariable Long ruleId,
                                @RequestBody @Validated AuditRuleForm ruleForm) {
        sysAuditRuleService.updateRule(ruleId, ruleForm);
        return Result.success();
    }

    @Operation(summary = "删除规则", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:auditRule:delete')")
    public Result<?> deleteRules(@Parameter(description = "规则ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = sysAuditRuleService.deleteRules(ids);
        return Result.judge(result);
    }
}
